﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace BUS
{
    public class Utilities
    {
        private static Dictionary<char, char> dictionary = new Dictionary<char, char>
            {
                { 'á', 'a' },
                { 'à', 'a' },
                { 'ả', 'a' },
                { 'ã', 'a' },
                { 'ạ', 'a' },
                { 'â', 'a' },
                { 'ấ', 'a' },
                { 'ầ', 'a' },
                { 'ẩ', 'a' },
                { 'ẫ', 'a' },
                { 'ậ', 'a' },
                { 'ă', 'a' },
                { 'ắ', 'a' },
                { 'ằ', 'a' },
                { 'ẳ', 'a' },
                { 'ẵ', 'a' },
                { 'ặ', 'a' },
                { 'é', 'e' },
                { 'è', 'e' },
                { 'ẻ', 'e' },
                { 'ẽ', 'e' },
                { 'ẹ', 'e' },
                { 'ê', 'e' },
                { 'ế', 'e' },
                { 'ề', 'e' },
                { 'ể', 'e' },
                { 'ễ', 'e' },
                { 'ệ', 'e' },
                { 'í', 'i' },
                { 'ì', 'i' },
                { 'ỉ', 'i' },
                { 'ĩ', 'i' },
                { 'ị', 'i' },
                { 'ó', 'o' },
                { 'ò', 'o' },
                { 'ỏ', 'o' },
                { 'õ', 'o' },
                { 'ọ', 'o' },
                { 'ô', 'o' },
                { 'ố', 'o' },
                { 'ồ', 'o' },
                { 'ổ', 'o' },
                { 'ỗ', 'o' },
                { 'ộ', 'o' },
                { 'ơ', 'o' },
                { 'ớ', 'o' },
                { 'ờ', 'o' },
                { 'ở', 'o' },
                { 'ỡ', 'o' },
                { 'ợ', 'o' },
                { 'ú', 'u' },
                { 'ù', 'u' },
                { 'ủ', 'u' },
                { 'ũ', 'u' },
                { 'ụ', 'u' },
                { 'ư', 'u' },
                { 'ứ', 'u' },
                { 'ừ', 'u' },
                { 'ử', 'u' },
                { 'ữ', 'u' },
                { 'ự', 'u' },
                { 'ý', 'y' },
                { 'ỳ', 'y' },
                { 'ỷ', 'y' },
                { 'ỹ', 'y' },
                { 'ỵ', 'y' },
                { 'đ', 'd' },
            };

        public static string FormatInputData(string strInput)
        {
            string strOutput = String.Empty;
            try
            {
                string strLowerInput = strInput.ToLower();
                strLowerInput = strLowerInput.Trim();
                strLowerInput = Regex.Replace(strLowerInput, @"\s+", " ");
                foreach (char c in strLowerInput)
                {
                    if (dictionary.ContainsKey(c))
                    {
                        strOutput += dictionary[c];
                    }
                    else
                    {
                        strOutput += c;
                    }
                }
            }
            catch (System.Exception ex)
            {
                strOutput = String.Empty;
            }

            return strOutput;
        }

        public static bool CheckExistNumber(string strInput)
        {
            bool ret = false;
            try
            {
                ret = Regex.IsMatch(strInput, @"\d");
            }
            catch (System.Exception ex)
            {
                ret = false;
            }

            return ret;
        }

        public static void WriteLog(string strLogText)
        {
            try
            {
                StreamWriter log = null;
                if (!File.Exists("log.txt"))
                {
                    log = new StreamWriter("log.txt");
                }
                else
                {
                    log = File.AppendText("log.txt");
                }

                log.WriteLine(DateTime.Now);
                log.WriteLine(strLogText);
                log.WriteLine();

                log.Dispose();
                log.Close();
            }
            catch (System.Exception ex)
            {
            	
            }
        }
    }
}
